package com.coder.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.rental.entity.CarMaker;
import com.coder.rental.service.ICarMakerService;
import com.coder.rental.utils.PinYinUtil;
import com.coder.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 汽车厂商 前端控制器
 * </p>
 *
 * @author zhaojj
 * @since 2025-01-10
 */
@RestController
@RequestMapping("/rental/carMaker")
public class CarMakerController {
    @Resource
    private ICarMakerService carMakerService;
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody CarMaker carMaker) {
        Page<CarMaker> page = carMakerService.search(start, size, carMaker);
        return Result.success(page);
    }

//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable Integer id) {
//        return carMakerService.removeById(id) ? Result.success() : Result.fail();
//    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        return carMakerService.removeByIds(list) ? Result.success() : Result.fail();
    }

    @PostMapping
    public Result save(@RequestBody CarMaker carMaker) {
        carMaker.setOrderLetter(PinYinUtil.getPinYin(carMaker.getName()));
        return carMakerService.save(carMaker) ? Result.success() : Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody CarMaker carMaker) {
        carMaker.setOrderLetter(PinYinUtil.getPinYin(carMaker.getName()));
        return carMakerService.updateById(carMaker) ? Result.success() : Result.fail();
    }

    @GetMapping
    public Result selectAll() {
        List<CarMaker> list = carMakerService.list();
        return Result.success(list);
    }

    @GetMapping("hasBrand/{makerId}")
    public Result hasBrand(@PathVariable Integer makerId) {
        return carMakerService.hasBrand(makerId) ? Result.success().setMessage("有") : Result.success().setMessage("无");
    }
}
